package com.wxsoft.business.common.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.alibaba.fastjson.JSONObject;
import com.wxsoft.business.model.easyui.Json;
import com.wxsoft.business.sys.pojo.User;
import com.wxsoft.util.common.JsonTools;

public class BaseController {

	/**
	 * hibernate validator校验是返回使用
	 * @param fieldError
	 * @return
	 */
	public JSONObject ResponseJsonError(FieldError fieldError) {
		String message = fieldError.getDefaultMessage();
		JSONObject response = new JSONObject();
		
		if (message != null) {
			response.put("result", "error");
			response.put("field", fieldError.getField());
			response.put("msg", message);
		} 
		return response;
	}
	/**
	 * 返回错误
	 * @param request
	 * @param response
	 * @param bindingResult
	 */
	public void writeReturn(HttpServletRequest request,HttpServletResponse response, BindingResult bindingResult){
		StringBuffer sb = new StringBuffer();
		List<FieldError> fieldErrors = bindingResult.getFieldErrors();
		List<JSONObject> fieldL = new ArrayList<JSONObject>();
		for(FieldError fieldError : fieldErrors){
			sb.append(fieldError.getField()+fieldError.getDefaultMessage());
			sb.append("");
			JSONObject jsonO = new JSONObject();
			jsonO.put("field", fieldError.getField());
			jsonO.put("errorMsg", fieldError.getDefaultMessage());
			fieldL.add(jsonO);
		}

		Json j = new Json();
		j.setSuccess(false);
		j.setMsg(sb.toString());
		j.setObj(fieldL);

		this.write(request,response,j);
		
	}
	/**
	 * 返回前台结果
	 * 
	 * @param response
	 * @param m
	 */
	public void writeReturn(HttpServletRequest request,HttpServletResponse response, String m) {
		try {
			Json j = new Json();
			j.setSuccess(true);
			j.setMsg(m);

			write(request,response,j);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}	
	public void writeReturn(HttpServletRequest request,HttpServletResponse response, boolean r, String m) {
		try {
			Json j = new Json();
			j.setSuccess(r);
			j.setMsg(m);
			write(request,response,j);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 输出json
	 * 
	 * @param response
	 * @param object
	 */
//	public void write(HttpServletResponse response, Object object) {
//		try {
//			// response.setContentType("text/json;charset=" + charset);
//			// PrintWriter out = response.getWriter();
//			// out.write(json);
//			// out.close();
//			response.setCharacterEncoding("UTF-8");
//			response.setContentType("text/json;charset=UTF-8");
//			//response.setContentType("application/json;charset=UTF-8");
//			response.getWriter().write(JsonTools.createJsonString(object));
//			response.getWriter().flush();
//			response.getWriter().close();
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//	}
	public void write(HttpServletRequest request,HttpServletResponse response, Object object) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/json;charset=UTF-8");
			String Agent = request.getHeader("User-Agent");
			System.out.println(Agent);
			if(!(Agent.indexOf("IE")<0)){
				response.setContentType("application/json;charset=UTF-8");
			}
			response.getWriter().write(JsonTools.createJsonString(object));
			response.getWriter().flush();
			response.getWriter().close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 输出json
	 * 
	 * @param response
	 * @param json
	 */
	public void write(HttpServletResponse response, String json) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/json");
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 返回结果消息
	 * 
	 * @param response
	 * @param bool
	 */
	public void write(HttpServletResponse response, boolean bool) {
		try {
			String json = "{\"success\":\"" + bool + "\"}";
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static String getloginName(HttpServletRequest request,
			HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute("USER");
		return user.getUsername();
	}
	public static Integer getloginId(HttpServletRequest request,
			HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute("USER");
		return user.getId();
	}
}
